import * as actionTypes from "./constants";

import {
  getTopBanners,
  getHotRecommends,
  getNewAlbums,
  getSongListById,
} from "@/service/recommend";

const changeTopBannerAction = (res) => ({
  type: actionTypes.TOP_BANNER_ACTION,
  topBanners: res.banners,
});

const changeRecommendAction = (res) => ({
  type: actionTypes.RECPMMEND_ACTION,
  hotRecommends: res.result,
});

const changeNewAlbumsAction = (res) => ({
  type: actionTypes.NEWALBUMS_ACTION,
  newAlbums: res.weekData,
});

const changeSongListAction = (res) => ({
  type: actionTypes.GET_SONG_LIST_ACTION,
  songList: res.playlist.tracks,
});

export const getTopBannersAction = () => {
  return (dispatch) => {
    getTopBanners().then((res) => {
      dispatch(changeTopBannerAction(res));
    });
  };
};

export const getHotRecommendsAction = (limit) => {
  return (dispatch) => {
    getHotRecommends(limit).then((res) => {
      dispatch(changeRecommendAction(res));
    });
  };
};

export const getNewAlbumsAction = (limit) => {
  return (dispatch) => {
    getNewAlbums(limit).then((res) => {
      dispatch(changeNewAlbumsAction(res));
    });
  };
};

export const getSongListAction = () => {
  return (dispatch) => {
    getSongListById().then((res) => {
      dispatch(changeSongListAction(res));
    });
  };
};
